#include "ChatServiceImpl.h"

Status ChatServiceImpl::NotifyAddFriend(ServerContext* context, const AddFriendReq* request, AddFriendRsp* response)
{
    auto to_uid = request->touid();
    auto session = UserMgr::GetInstance()->GetSession(to_uid);
    Defer defer([response, request]() {
        response->set_applyuid(request->applyuid());
        response->set_error(ErrorCodes::SUCCESS);
        response->set_touid(request->touid());
        });
    if (session == nullptr) {
        return Status::OK;
    }
    
    Json::Value rtvalue;
    rtvalue["error"] = ErrorCodes::SUCCESS;
    rtvalue["applyname"] = request->name();
    rtvalue["applyuid"] = request->applyuid();
    rtvalue["icon"] = request->icon();
    rtvalue["desc"] = request->desc();
    rtvalue["nick"] = request->nick();
    rtvalue["sex"] = request->sex();

    auto str_send = rtvalue.toStyledString();
    session->Send(str_send,ID_NOTIFY_ADD_FRIEND_REQ);


    return Status::OK;
}

Status ChatServiceImpl::NotifyAuthFriend(ServerContext* context, const AuthFriendReq* request, AuthFriendRsp* response)
{
    auto toid = request->touid();
    auto fromid = request->fromuid();

    auto ses = UserMgr::GetInstance()->GetSession(toid);

    Defer defer([response,request]() {
        response->set_error(ErrorCodes::SUCCESS);
        response->set_fromuid(request->fromuid());
        response->set_touid(request->touid());
        });
    if (!ses) {
        response->set_error(ErrorCodes::UidInvalid);
    }
    Json::Value rtroot;
    rtroot["error"] = ErrorCodes::SUCCESS;
    rtroot["applyuid"] = request->fromuid();
    rtroot["toid"] = request->touid();

    std::string base_key = USER_BASE_INFO + std::to_string(request->fromuid());
    auto userinfo = std::make_shared<UserInfo>();
    bool b_base = GetBaseInfo(base_key,request->fromuid(), userinfo);
    if (!b_base) {
        rtroot["error"] = ErrorCodes::UidInvalid;
    }
    rtroot["applyname"] = userinfo->_name;
    rtroot["nick"] = userinfo->_nick;
    rtroot["icon"] = userinfo->_icon;
    rtroot["sex"] = userinfo->_sex;
    rtroot["desc"] = userinfo->_desc;

    std::string info_str = rtroot.toStyledString();
    ses->Send(info_str, ID_NOTIFY_AUTH_FRIEND_REQ);

    return Status::OK;
}

bool ChatServiceImpl::GetBaseInfo(std::string base_key, int uid, std::shared_ptr<UserInfo>& userinfo)
{
    std::string base_info = "";
    bool b_get = RedisMgr::GetInstance()->Get(base_key, base_info);
    if (b_get) {
        Json::Value root;
        Json::Reader parse;
        parse.parse(base_info, root);
        userinfo->_uid = root["uid"].asInt();
        userinfo->_name = root["name"].asString();
        userinfo->_password = root["pwd"].asString();
        userinfo->_email = root["email"].asString();
        userinfo->_nick = root["nick"].asString();
        userinfo->_desc = root["desc"].asString();
        userinfo->_sex = root["sex"].asInt();
        userinfo->_icon = root["icon"].asString();
    }
    else {
        std::shared_ptr<UserInfo> user = nullptr;
        user = MySqlMgr::GetInstance()->GetUser(uid);
        userinfo = user;

        Json::Value redis_root;
        redis_root["uid"] = uid;
        redis_root["pwd"] = userinfo->_password;
        redis_root["name"] = userinfo->_name;
        redis_root["email"] = userinfo->_email;
        redis_root["nick"] = userinfo->_nick;
        redis_root["desc"] = userinfo->_desc;
        redis_root["sex"] = userinfo->_sex;
        redis_root["icon"] = userinfo->_icon;
        RedisMgr::GetInstance()->Set(base_key, redis_root.toStyledString());
    }
    return true;
}

Status ChatServiceImpl::NotifyTextChatMsg(::grpc::ServerContext* context, const TextChatMsgReq* request, TextChatMsgRsp* response)
{
    auto to_uid = request->touid();
    auto session = UserMgr::GetInstance()->GetSession(to_uid);
    response->set_error(ErrorCodes::SUCCESS);
    if (!session) {
        return Status::OK;
    }

    Json::Value rtroot;
    rtroot["fromuid"] = request->fromuid();
    rtroot["touid"] = to_uid;
    rtroot["error"] = ErrorCodes::SUCCESS;

    Json::Value text_array;
    for (auto& msg : request->textmsgs()) {
        Json::Value ele;
        ele["content"] = msg.msgcontent();
        ele["msgid"] = msg.msgid();
        text_array.append(ele);
    }
    rtroot["text_array"] = text_array;

    std::string str_rt = rtroot.toStyledString();

    session->Send(str_rt, ID_NOTIFY_TEXT_CHAT_MSG_REQ);
    return Status::OK;

}
